
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for special_page.
	''' </summary>
	Public Partial Class special_page
		Inherits BaseWebPage
		Protected lblSeparator As System.Web.UI.WebControls.Label
		Protected id As Integer = -1
		Protected period As DateTime = DateTime.Now
		Protected numItems As Integer = Constants.MUSIC_ARTICLECOLUMN_MAX
		Protected numInterviewItems As Integer = Constants.MUSIC_INTERVIEWSCOLUMN_MAX
		Protected numContentItems As Integer = Constants.MUSIC_CONTENTCOLUMN_MAX
		Protected hlItemGenre As System.Web.UI.WebControls.HyperLink
		Protected usr As User
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.SPECIAL_PAGE;

			usr = LoggedUser

			'			if(usr == null)
			'				Response.Redirect(Constants.LOGIN_PAGE);

			If Constants.SHOW_MUSIC <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			' init special page
			If Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = Nothing OrElse Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			id = Convert.ToInt32(Request.Params(Constants.SPECIAL_PAGE_TYPE_ID_PARAM))
			If id = 0 Then
				Dim music As BO.SpecialPage = BO.SpecialPages.GetSpecialPageByName(Constants.MUSIC_PAGE)
				If music <> Nothing Then
					id = music.ID
				End If
			End If

			Dim CurrentPage As SpecialPage = SpecialPages.GetSpecialPageByID(id)

			If CurrentPage = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Me.initPageTitles(Constants.SPECIAL, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucMusicTabs.ActiveTab = 1
			ucMusicTabs.PageName = Constants.SPECIAL

			' init page title
			litPageName.Text = CurrentPage.Name

			' init tab links
			hlInterviewsViewMore.NavigateUrl = "../" + Constants.VIEW_ALL_NEWS_PAGE + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritInterview)
			hlNewsViewMore.NavigateUrl = "../" + Constants.VIEW_ALL_NEWS_PAGE + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritNews)
			hlMTViewMore.NavigateUrl = "../" + Constants.VIEW_ALL_CONTENT_ITEMS_PAGE + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id

			' init period and last month posted
			period = SpecialPage.GetLastMonth(id)
			Dim curDate As DateTime = period
			'string sPeriod = "";

			If Request.Params("p") <> Nothing AndAlso Request.Params("p") <> "" Then
				Dim ppTemp As String = Request.Params("p").ToString()
				Dim mandyear As String() = ppTemp.Split("_"C)
				Dim yearID As Integer = Convert.ToInt32(mandyear(0))
				Dim monthID As Integer = Convert.ToInt32(mandyear(1))

				hlInterviewsViewMore.NavigateUrl = String.Format("{0}&p={1}", hlInterviewsViewMore.NavigateUrl, ppTemp)
				hlNewsViewMore.NavigateUrl = String.Format("{0}&p={1}", hlNewsViewMore.NavigateUrl, ppTemp)
				hlMTViewMore.NavigateUrl = String.Format("{0}&p={1}", hlMTViewMore.NavigateUrl, ppTemp)
				' for last month always show top N items, for all other months all items for the month
				'				if( period.Month != monthID || period.Year != yearID)
'				{
'					numItems = -1;
'					numInterviewItems = -1;
'					numContentItems = -1;
'				}
'

				period = New DateTime(yearID, monthID, 1)
				'DateTime tempPeriod = new DateTime(yearID, monthID, 1);
				'if( tempPeriod < period)
				'	period = tempPeriod;
				'sPeriod = Request.Params["p"].ToString();

				' init "No news" and "No inreviews" labels
				Dim nCurMonth As Integer = period.Month
				If nCurMonth <> monthID Then
					lblNoMiddleItems.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgNoArchivedNews_TEXT")
					lblNoArticleItems.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgNoArchivedInterviews_TEXT")
				End If
			Else
				'				if( Request.Params["all"] != null && Request.Params["all"] == "true")
'				{
'					numItems = -1;
'					numInterviewItems = -1;
'				}
'
				'period = new DateTime(period.Year, period.Month, 1);
					'Common.DateUtilities.GetStartOfQuarter( period.Year, Common.DateUtilities.GetQuarter((Common.Month)period.Month )) ;
				period = Common.DateUtilities.GetStartOfCurrentQuarter()
			End If

			If Not Me.IsPostBack Then
				FillQuartersDropDown()
				BindRightGrid()
				BindLeftGrid()
				BindMiddleGrid()
			End If

			' init Add links
			If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
				hlAddContentItem.NavigateUrl = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritInterview)
				hlAddNews.NavigateUrl = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&col=" + Convert.ToInt32(ArticleItem.ArticleItemType.ritNews)
				hlAddArticleItem.NavigateUrl = "../" + Constants.ADD_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id
			Else
				hlAddContentItem.Visible = False
				hlAddNews.Visible = False
				hlAddArticleItem.Visible = False
			End If
		End Sub

		' Right column
		Protected Function CreateRightDataSource() As ICollection
			Dim articleItems As ArrayList = SpecialPage.GetArticleItems(id, Convert.ToInt32(ArticleItem.ArticleItemType.ritInterview), period, numItems, -1, -1)

			Dim dt As New DataTable()

			If articleItems.Count <= 0 Then
				lblNoArticleItems.Visible = True
				dgMusicUsers.Visible = False
				hlInterviewsViewMore.Visible = False
			Else
				Dim dr As DataRow

				dt.Columns.Add(New DataColumn("UserName", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
				dt.Columns.Add(New DataColumn("PhotoURL", GetType(String)))
				dt.Columns.Add(New DataColumn("NewsURL", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewTitle", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewSubtitle", GetType(String)))
				dt.Columns.Add(New DataColumn("InterviewContent", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLink", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("NewsID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))

				Dim user As User = Nothing
				Dim grp As Group = Nothing
				Dim pic As UserPicture
				Dim grpPic As GroupPicture
				For Each result As ArticleItem In articleItems
					dr = dt.NewRow()

					If result.UserID > 0 Then
						user = BO.Users.GetUserByID(result.UserID)
						dr(0) = user.Name

						dr(1) = Users.ComposeMemeberLink(user)
						dr(10) = Users.ComposeMemeberLinkOnClickJS(user, trUserCanceledMessage)

						pic = user.DefaultPicture
						If pic <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(pic, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
ElseIf result.GroupID > 0 Then
						grp = BO.Groups.GetGroupByID(result.GroupID)
						dr(0) = grp.Name
						dr(1) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
						grpPic = grp.DefaultPicture
						If grpPic <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(grpPic, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
					End If
					dr(3) = "../" + Constants.VIEW_NEWS + "?id=" + result.ID + "&" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&col=" + Convert.ToInt32(result.Type)
					dr(4) = HttpUtility.HtmlEncode(result.Title)
					'Server.HtmlDecode(result.Title);
					dr(5) = HttpUtility.HtmlEncode(result.Subtitle)
					'Server.HtmlDecode(result.Subtitle);
					dr(6) = special_page.getNewsStart(Server.HtmlDecode(result.Content))
					dr(7) = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(8) = False
					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(8) = True
					End If
					dr(9) = result.ID
					' init with newsID
					dt.Rows.Add(dr)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindRightGrid()
			'init datagrid with database data ordered param
			dgMusicUsers.DataSource = CreateRightDataSource()
			dgMusicUsers.DataBind()
		End Sub

		' Left column
		' test code for left and middle columns
		Protected Function CreateLeftDataSource() As ICollection
			Dim contentItems As ArrayList = SpecialPage.GetContentItems(id, period, numContentItems, -1, -1)

			Dim dt As New DataTable()

			Dim contentItemsCount As Integer = contentItems.Count
			dgContentItems.AllowSorting = True

			If contentItemsCount <= 0 Then
				lblNoContentItems.Visible = True
				dgContentItems.Visible = False
				hlMTViewMore.Visible = False
			Else

				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("MemberName", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberImageURL", GetType(String)))
				dt.Columns.Add(New DataColumn("URL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("ItemURL", GetType(String)))
				dt.Columns.Add(New DataColumn("ItemName", GetType(String)))
				'dt.Columns.Add(new DataColumn("ItemGenreName", typeof(string)));
				dt.Columns.Add(New DataColumn("Think", GetType(String)))
				dt.Columns.Add(New DataColumn("EditItemLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("EditItemLink", GetType(String)))
				dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberFullName", GetType(String)))

				' List left items
				Dim user As User = Nothing
				Dim pic As UserPicture
				Dim grp As Group = Nothing
				Dim grpPic As GroupPicture
				Dim tmpLinkType As New LinkTypes.LinkType(-1, "")

				For Each result As ContentItem In contentItems
					dr = dt.NewRow()

					If result.UserID > 0 Then
						user = BO.Users.GetUserByID(result.UserID)
						dr(0) = Me.getBeginning(HttpUtility.HtmlEncode(user.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
						pic = user.DefaultPicture
						If pic <> Nothing Then
							dr(1) = Pictures.GetPictureSrcFullURL(pic, True, "../")
						Else
							dr(1) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If

						dr(2) = Users.ComposeMemeberLink(user)
						dr(9) = Users.ComposeMemeberLinkOnClickJS(user, trUserCanceledMessage)

						dr(10) = user.Name
ElseIf result.GroupID > 0 Then
						grp = BO.Groups.GetGroupByID(result.GroupID)
						dr(0) = Me.getBeginning(HttpUtility.HtmlEncode(grp.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
						grpPic = grp.DefaultPicture
						If grpPic <> Nothing Then
							dr(1) = Pictures.GetPictureSrcFullURL(grpPic, True, "../")
						Else
							dr(1) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If
						dr(2) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
						dr(10) = grp.Name
					End If
					dr(3) = result.ID
					dr(4) = "../" + Constants.VIEW_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&id=" + result.ID
					dr(5) = HttpUtility.HtmlEncode(result.LinkName)
					If result.LinkName = "" AndAlso result.LinkURL <> "" Then
						dr(5) = result.LinkURL
					End If

					dr(6) = Me.getBeginning(result.Content, Constants.MAX_CONTENT_ITEM_DESC_LENGTH)
					dr(7) = False

					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(7) = True
					End If
					dr(8) = "../" + Constants.ADD_CONTENT_ITEM + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&id=" + result.ID

					dt.Rows.Add(dr)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindLeftGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgContentItems.DataSource = CreateLeftDataSource()
			dgContentItems.DataBind()
		End Sub

		' Middle column
		Protected Function CreateMiddleDataSource() As ICollection
			Dim middleItems As ArrayList = SpecialPage.GetArticleItems(id, Convert.ToInt32(ArticleItem.ArticleItemType.ritNews), period, numInterviewItems, -1, -1)

			Dim dt As New DataTable()

			Dim middleItemsCount As Integer = middleItems.Count
			dgMiddleItems.AllowSorting = True

			If middleItemsCount <= 0 Then
				lblNoMiddleItems.Visible = True
				dgMiddleItems.Visible = False
				hlNewsViewMore.Visible = False
			Else
				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("NewsTitle", GetType(String)))
				dt.Columns.Add(New DataColumn("NewsText", GetType(String)))
				dt.Columns.Add(New DataColumn("FullTextLinkURL", GetType(String)))
				dt.Columns.Add(New DataColumn("FullNewsLinkText", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLink", GetType(String)))
				dt.Columns.Add(New DataColumn("EditLinkVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("NewsID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("NewsSubtitle", GetType(String)))

				' List news
				For Each result As ArticleItem In middleItems
					dr = dt.NewRow()
					dr(0) = HttpUtility.HtmlEncode(result.Title)
					'Server.HtmlDecode(result.Title);
					dr(1) = special_page.getNewsStart(Server.HtmlDecode(result.Content))
					dr(2) = "../" + Constants.VIEW_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(3) = "More &gt;"
					dr(4) = "../" + Constants.ADD_NEWS + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id + "&id=" + result.ID + "&col=" + Convert.ToInt32(result.Type)
					dr(5) = False
					If usr <> Nothing AndAlso usr.Status = Constants.appAdmin Then
						dr(5) = True
					End If
					dr(6) = result.ID
					' init with newsID
					dr(7) = HttpUtility.HtmlEncode(result.Subtitle)
					'Server.HtmlDecode(result.Subtitle);
					dt.Rows.Add(dr)
				Next
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindMiddleGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgMiddleItems.DataSource = CreateMiddleDataSource()
			dgMiddleItems.DataBind()
		End Sub

		Protected Sub FillQuartersDropDown()
			ddlQuarters.ClearSelection()
			'ddlQuarters.Items.Add(new ListItem(" -- Choose period -- ", string.Empty));
			Dim i As Integer = 0
			While i < Constants.MAX_QUARTER
				Dim startPeriod As DateTime = Common.DateUtilities.GetStartOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				'(Common.Month)(Common.DateUtilities.GetStartOfCurrentQuarter().Month - 3*i) ) );
				Dim endPeriod As DateTime = Common.DateUtilities.GetEndOfQuarter(DateTime.Now.AddMonths(-3 * i).Year, Common.DateUtilities.GetQuarter(DirectCast(DateTime.Now.AddMonths(-3 * i).Month, Common.Month)))
				'(Common.Month)(Common.DateUtilities.GetEndOfCurrentQuarter().Month - 3*i) ) );
				ddlQuarters.Items.Add(New ListItem(String.Format("{0} - {1}", startPeriod.ToString("MMM, yyyy"), endPeriod.ToString("MMM, yyyy")), String.Format("{0}_{1}", startPeriod.Year, startPeriod.Month)))
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
			'if( period != Common.DateUtilities.GetEndOfCurrentQuarter() )
			ddlQuarters.SelectedIndex = ddlQuarters.Items.IndexOf(ddlQuarters.Items.FindByValue(String.Format("{0}_{1}", period.Year, period.Month)))
			'else
			'	ddlQuarters.SelectedIndex = 0;
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMusicUsers.ItemCommand, AddressOf Me.dgMiddleItems_ItemCommand
			AddHandler Me.dgMiddleItems.ItemCommand, AddressOf Me.dgMiddleItems_ItemCommand
			AddHandler Me.dgContentItems.ItemCommand, AddressOf Me.dgContentItems_ItemCommand

		End Sub
#End Region

		Protected Sub ddlQuarters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(String.Format("{0}?{1}={2}&p={3}", Constants.SPECIAL, Constants.SPECIAL_PAGE_TYPE_ID_PARAM, id, ddlQuarters.SelectedValue))
			'AutoPostBack="false"
		End Sub

		Private Sub dgMiddleItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			If cmd = "DeleteNews" Then
				Dim newsInterview As ArticleItem = ArticleItem.GetArticleItemByID(Convert.ToInt32(e.CommandArgument))
				If newsInterview <> Nothing Then
					newsInterview.DoDelete()
				End If
				Response.Redirect(Constants.SPECIAL + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id)
			End If
		End Sub

		Private Sub dgContentItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			If cmd = "DeleteItem" Then
				Dim contentItem As ContentItem = ContentItem.GetContentItemByID(Convert.ToInt32(e.CommandArgument))
				If contentItem <> Nothing Then
					contentItem.DoDelete()
				End If
				Response.Redirect(Constants.SPECIAL + "?" + Constants.SPECIAL_PAGE_TYPE_ID_PARAM + "=" + id)
			End If
		End Sub

		Public Shared Function getNewsStart(ByVal sNews As String) As String
			If sNews.Length > Constants.MAX_NEWS_CHARS_ON_SPECIAL_PAGE Then
				Dim temp As String = ""
				Dim rest As String = ""
				Dim nDotIndex As Integer = -1
				temp = sNews.Substring(0, Constants.MAX_NEWS_CHARS_ON_SPECIAL_PAGE)
				rest = sNews.Substring(Constants.MAX_NEWS_CHARS_ON_SPECIAL_PAGE, sNews.Length - Constants.MAX_NEWS_CHARS_ON_SPECIAL_PAGE)
				nDotIndex = rest.IndexOf(".")

				If nDotIndex <> -1 Then
					rest = rest.Substring(0, nDotIndex)
					temp = temp + rest + "..."
				Else
					temp += "..."
				End If
				sNews = temp
			End If
			Return sNews
		End Function

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






